package com.itbuka.user.controller;

import com.itbuka.domain.User;
import com.itbuka.entity.StatusCode;
import com.itbuka.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.itbuka.entity.Result;
import com.itbuka.entity.PageResult;
import com.github.pagehelper.Page;

import java.util.List;

/**
 * 用户表
 */
@RestController
@RequestMapping("/User")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 查询全部用户表
     */
    @GetMapping("/selectAll")
    public Result<List<User>> selectAll() {
        return Result.ok("查询成功", userService.selectAll());
    }

    /**
     * 条件查询用户表
     */
    @PostMapping("/selectList")
    public Result<List<User>> select(@RequestBody User user) {
        return Result.ok("条件查询成功", userService.selectList(user));
    }

    /**
     * 新增用户表
     */
    @PostMapping("/insert")
    public Result<?> insert(@RequestBody User user) {
        userService.insert(user);
        return Result.ok("新增成功");
    }

    /**
     * 删除用户表
     */
    @GetMapping("/delete")
    public Result<?> delete(@RequestParam String ids) {
        userService.delete(ids);
        return Result.ok("删除成功");
    }

    /**
     * 修改用户表
     */
    @PostMapping("/update")
    public Result<?> update(@RequestBody User user) {
        userService.update(user);
        return Result.ok("修改成功");
    }

    /**
     * 分页搜索实现用户表
     */
    @GetMapping(value = "/pageAll")
    public Result<?> pageAll(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size) {
        Page<User> pageList = userService.pageAll(page, size);
        PageResult<?> pageResult = new PageResult<>(pageList.getTotal(), pageList.getResult());
        return Result.ok("查询成功", pageResult);
    }

    /**
     * 分页条件搜索实现用户表
     */
    @GetMapping(value = "/pageList")
    public Result<?> pageList(@RequestBody User user, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size) {
        Page<User> pageList = userService.pageList(user, page, size);
        PageResult<?> pageResult = new PageResult<>(pageList.getTotal(), pageList.getResult());
        return Result.ok("查询成功", pageResult);
    }

    /**
     * 用户启用、停用
     */
    @GetMapping("/status")
    public Result<?> status(@RequestParam Long id, @RequestParam Integer status) {
        Integer integer = userService.status(id, status);
        if (integer != 1) {
            return new Result<>(false, StatusCode.ERROR, "用户状态修改失败");
        }
        return new Result<>(true, StatusCode.OK, "用户状态修改成功");
    }

    /**
     * 重置密码
     */
    @PostMapping("/reset")
    public Result<?> reset(@RequestBody List<Long> ids) {
        userService.resetPwd(ids);
        return new Result("修改成功");
    }

    /**
     * 登录
     */
    @PostMapping("/login")
    public Result<?> login(@RequestBody User user) {
        String login = userService.login(user.getUsername(), user.getPassword());
        if (login.equals("-1")) {
            return Result.fail("用户名或密码错误");
        } else if (login.equals("-2")) {
            return Result.fail("该用户不存在");
        } else if (login.equals("-3")) {
            return Result.fail("该用户已停用");
        }
        return Result.fail("登陆成功",login);
    }
}
